<template>
  <!-- v-if=" $route.query.immunityAuditType == 1 || form.immunityAuditType == 1" -->
  <div>
    <!-- 担保人信息 -->
    <div class="card-wrap flex-column justify-end card-padding" v-if="$route.query.immunityAuditType == 1 || form.immunityAuditType == 1">
      <div class="card-title-wrap">
        <span>担保人信息</span>
        <span></span>
      </div>
      <el-form :model="form" ref="surety" label-width="180px" :label-position="type == 'detail' ? 'right' : 'top'" :disabled="type == 'detail'">
        <div :style="{ marginLeft: type == 'detail' ? '' : '50px' }">
          <el-tabs v-model="personage">
            <el-tab-pane label="个人担保" name="personage"></el-tab-pane>
          </el-tabs>
          <el-table :data="form.supplementSuretyPersonalList">
            <el-table-column label="姓名" align="center" :render-header="addRedStar">
              <template #default="{ row, $index }">
                <el-form-item label-width="0" :prop="'supplementSuretyPersonalList.' + $index + '.suretyName'" :rules="{ required: true, trigger: 'blur', message: '请输入姓名' }">
                  <el-input maxlength="10" v-model="row.suretyName" placeholder="请输入内容" style="width: 100%"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="身份证号" align="center" :render-header="addRedStar" min-width="100px" show-overflow-tooltip>
              <template #default="{ row, $index }">
                <el-form-item label-width="0" :prop="'supplementSuretyPersonalList.' + $index + '.idCardNo'" :rules="{ required: true, trigger: 'blur', message: '请输入身份证号', validator: checkIdCard }">
                  <el-input v-model="row.idCardNo" placeholder="请输入内容" maxlength="18"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="联系电话" align="center" :render-header="addRedStar">
              <template #default="{ row, $index }">
                <el-form-item label-width="0" :prop="'supplementSuretyPersonalList.' + $index + '.contactMobile'" :rules="{ required: true, trigger: 'blur', message: '请输入联系电话', validator: checkTel }">
                  <el-input v-model="row.contactMobile" maxlength="11" placeholder="请输入内容"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="联系地址" align="center" :render-header="addRedStar" min-width="100px" show-overflow-tooltip>
              <template #default="{ row, $index }">
                <el-form-item label-width="0" :prop="'supplementSuretyPersonalList.' + $index + '.contactAddr'" :rules="{ required: true, trigger: 'blur', message: '请输入联系地址' }">
                  <el-input v-model="row.contactAddr" maxlength="50" placeholder="请输入内容"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="与客户关系" align="center" :render-header="addRedStar" min-width="100px" show-overflow-tooltip>
              <template #default="{ row, $index }">
                <el-form-item label-width="0" :prop="'supplementSuretyPersonalList.' + $index + '.relation'" :rules="{ required: true, trigger: 'blur', message: '请输入与客户关系' }">
                  <el-input v-model="row.relation" maxlength="30" placeholder="请输入内容"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="电子邮箱" align="center" :render-header="addRedStar" min-width="100px" show-overflow-tooltip>
              <template #default="{ row, $index }">
                <el-form-item label-width="0" :prop="'supplementSuretyPersonalList.' + $index + '.email'" :rules="{ required: true, trigger: 'blur', message: '请输入电子邮箱' }">
                  <el-input v-model="row.email" placeholder="请输入内容"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="传真" align="center">
              <template slot-scope="scope">
                <el-form-item label-width="0">
                  <el-input v-model="scope.row.fax" maxlength="50" placeholder="请输入内容"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="" label="" width="50" v-if="type !== 'detail'">
              <template slot-scope="scope">
                <i class="el-icon-delete" style="cursor: pointer" @click="deleGr(scope)"></i>
              </template>
            </el-table-column>
          </el-table>
          <div style="text-align: center" v-if="type !== 'detail'">
            <el-button type="text" size="mini" icon="el-icon-plus" @click="addGuarantorGr">添加担保人</el-button>
          </div>
        </div>
        <div :style="{ marginLeft: type == 'detail' ? '' : '50px' }">
          <el-tabs v-model="firm">
            <el-tab-pane label="企业担保" name="firm"></el-tab-pane>
          </el-tabs>
          <el-table :data="form.supplementSuretyCorporateList">
            <el-table-column label="企业名称" align="center" :render-header="addRedStar">
              <template #default="{ row, $index }">
                <el-form-item :prop="'supplementSuretyCorporateList.' + $index + '.companyName'" :rules="{ required: true, trigger: 'blur', message: '请输入企业名称' }" label-width="0">
                  <el-input maxlength="50" v-model="row.companyName" placeholder="请输入内容" style="width: 100%"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="统一社会信用代码" align="center" :render-header="addRedStar" min-width="100px" show-overflow-tooltip>
              <template #default="{ row, $index }">
                <el-form-item label-width="0" :prop="'supplementSuretyCorporateList.' + $index + '.creditCode'" :rules="{ required: true, trigger: 'blur', message: '请输入统一社会信用代码' }">
                  <el-input v-model="row.creditCode" placeholder="请输入内容" maxlength="18"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="联系地址" align="center" :render-header="addRedStar" min-width="100px" show-overflow-tooltip>
              <template #default="{ row, $index }">
                <el-form-item label-width="0" :prop="'supplementSuretyCorporateList.' + $index + '.contactAddr'" :rules="{ required: true, trigger: 'blur', message: '请输入联系地址' }">
                  <el-input v-model="row.contactAddr" placeholder="请输入内容"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="联系人" align="center" :render-header="addRedStar">
              <template #default="{ row, $index }">
                <el-form-item label-width="0" :prop="'supplementSuretyCorporateList.' + $index + '.contactPerson'" :rules="{ required: true, trigger: 'blur', message: '请输入联系人' }">
                  <el-input v-model="row.contactPerson" placeholder="请输入内容"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="联系电话" align="center" :render-header="addRedStar">
              <template #default="{ row, $index }">
                <el-form-item label-width="0" :prop="'supplementSuretyCorporateList.' + $index + '.contactMobile'" :rules="{ required: true, trigger: 'blur', message: '请输入联系电话', validator: checkTel }">
                  <el-input v-model="row.contactMobile" maxlength="11" placeholder="请输入内容"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="与客户关系" align="center" :render-header="addRedStar">
              <template #default="{ row, $index }">
                <el-form-item label-width="0" :prop="'supplementSuretyCorporateList.' + $index + '.relation'" :rules="{ required: true, trigger: 'blur', message: '请输入与客户关系' }">
                  <el-input v-model="row.relation" placeholder="请输入内容"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="电子邮箱" align="center" :render-header="addRedStar" min-width="100px" show-overflow-tooltip>
              <template #default="{ row, $index }">
                <el-form-item label-width="0" :prop="'supplementSuretyCorporateList.' + $index + '.email'" :rules="{ required: true, trigger: 'blur', message: '请输入电子邮箱' }">
                  <el-input v-model="row.email" placeholder="请输入内容"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="传真" align="center">
              <template slot-scope="scope">
                <el-form-item label-width="0" prop="fax">
                  <el-input v-model="scope.row.fax" placeholder="请输入内容"></el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="" label="" width="50" v-if="type !== 'detail'">
              <template slot-scope="scope">
                <i class="el-icon-delete" style="cursor: pointer" @click="deleQy(scope)"></i>
              </template>
            </el-table-column>
          </el-table>
          <div style="text-align: center" v-if="type !== 'detail'">
            <el-button type="text" size="mini" icon="el-icon-plus" @click="addGuarantorQy">添加担保人</el-button>
          </div>
        </div>
      </el-form>
    </div>

    <!-- 担保人信息 -->
    <div class="card-wrap flex-column justify-end card-padding" v-if="$route.query.immunityAuditType == 2 || form.immunityAuditType == 2">
      <div class="card-title-wrap">
        <span>担保人信息</span>
        <span></span>
      </div>
      <el-form :model="formView" ref="surety" label-width="180px" disabled>
        <div style="margin-left: 10px">
          <el-tabs v-model="personage">
            <el-tab-pane label="个人担保" name="personage"></el-tab-pane>
          </el-tabs>
          <el-table :data="formView.personal">
            <el-table-column label="姓名" align="center" prop="name"></el-table-column>
            <el-table-column label="身份证号" align="center" prop="idNumber"></el-table-column>
            <el-table-column label="联系电话" align="center" prop="contactPhoneNumber"></el-table-column>
            <el-table-column label="联系地址" align="center" prop="contactAddress"> </el-table-column>
            <el-table-column label="与客户关系" align="center" prop="relationshipWithCustomers"> </el-table-column>
            <el-table-column label="电子邮箱" align="center" prop=""></el-table-column>
            <el-table-column label="传真" align="center" prop=""></el-table-column>
          </el-table>
        </div>
        <div style="margin-left: 10px">
          <el-tabs v-model="firm">
            <el-tab-pane label="企业担保" name="firm"></el-tab-pane>
          </el-tabs>
          <el-table :data="formView.enterprise">
            <el-table-column label="企业名称" align="center" prop="name"></el-table-column>
            <el-table-column label="统一社会信用代码" align="center" width="280" prop="idNumber"></el-table-column>
            <el-table-column label="联系地址" align="center" prop="contactAddress"></el-table-column>
            <el-table-column label="联系人" align="center" prop="contacts"></el-table-column>
            <el-table-column label="联系电话" align="center" prop="contactPhoneNumber"></el-table-column>
            <el-table-column label="与客户关系" align="center" prop="relationshipWithCustomers"></el-table-column>
            <el-table-column label="电子邮箱" align="center" prop="email"></el-table-column>
            <el-table-column label="传真" align="center" prop="fax"></el-table-column>
          </el-table>
        </div>
      </el-form>
    </div>
  </div>
</template>
<script>
import { getCreditReviewData } from '@/api/vehicleOrder'
export default {
  name: 'surety',
  dicts: ['car_status'],
  props: {
    value: {
      type: Object,
      default: {},
    },
    state: {
      type: String,
      default: '',
    },
  },
  computed: {
    form: {
      get() {
        let that = this
        const proxy = new Proxy(that.value, {
          get(target, key) {
            return Reflect.get(target, key)
          },
          set(target, key, value) {
            that.$emit('input', { ...target, [key]: value })
            return true
          },
        })
        return proxy
      },
    },
    type() {
      let status
      switch (this.state) {
        case 'write':
          status = 'edit'
          break
        case 'disabled':
          status = 'detail'
          break
        case 'readonly':
          status = 'detail'
          break
        case 'detail':
          status = 'detail'
          break
        case 'edit':
          status = 'edit'
        case 'add':
          status = 'add'
          break
      }
      return status
    },
  },
  created() { },
  components: {},
  methods: {},
  mounted() { },
  mounted() {
    console.log(this.$route.query.applicationNo, this.value.applicationNo, '00000000000000000000')
    this.getCreditReviewData(this.$route.query.applicationNo || this.value.applicationNo)
  },
  data() {
    return {
      formView: {},
      personage: 'personage',
      firm: 'firm',
    }
  },
  methods: {
    async getCreditReviewData(applicationNo) {
      try {
        let res = await getCreditReviewData(applicationNo)
        console.log(res.data, '000000000000000000')
        this.formView = res.data
      } catch (error) { }
    },
    addRedStar(h, { column }) {
      return [h('span', { style: 'color: red' }, '*'), h('span', ' ' + column.label)]
    },
    checkIdCard(rule, value, callback) {
      let regTel = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
      if (!value) {
        return callback(new Error('身份证不能为空'))
      } else if (!regTel.test(value)) {
        callback(new Error('请输入正确的身份证号'))
      } else {
        callback()
      }
    },
    checkTel(rule, value, callback) {
      let regTel = /^(((1[3456789][0-9]{1})|(15[0-9]{1}))+\d{8})$/
      if (!value) {
        return callback(new Error('电话号码不能为空'))
      } else if (!regTel.test(value)) {
        callback(new Error('请输入正确的电话号码'))
      } else {
        callback()
      }
    },
    addGuarantorGr() {
      this.form.supplementSuretyPersonalList.push({})
    },
    addGuarantorQy() {
      this.form.supplementSuretyCorporateList.push({})
    },
    deleGr(row) {
      this.form.supplementSuretyPersonalList.splice(row.$index, 1)
    },
    deleQy(row) {
      this.form.supplementSuretyCorporateList.splice(row.$index, 1)
    },
    valid() {
      return new Promise(async (resolve) => {
        let result = true
        let refs = Object.values(this.$refs)
        for (let index = 0; index < refs?.length; index++) {
          const ref = refs[index]
          if (Object.prototype.toString.call(ref) == '[object Array]') {
            for (let i = 0; i < ref.length; i++) {
              const element = ref[i]
              if (element?.valid) {
                let rc = await element?.valid?.()
                result = result && rc
              }
              if (element?.validate) {
                let rf = await this._valid(ref)
                result = result && rf
              }
              if (element?.form_validate) {
                let rf = await this._valid(ref, 'form_validate')
                result = result && rf
              }
            }
          } else {
            if (ref?.valid) {
              let rc = await ref?.valid?.()
              result = result && rc
            }
            if (ref?.validate) {
              let rf = await this._valid(ref)
              result = result && rf
            }
            if (ref?.form_validate) {
              let rf = await this._valid(ref, 'form_validate')
              result = result && rf
            }
          }
        }
        resolve(result)
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.box {
  width: 100%;
  .title {
    border-left: 3px solid #3280fc;
    padding-left: 5px;
    box-sizing: border-box;
  }
}
.app-container {
  // background-color: #fff;
}
</style>
